package server.config;

import server.HttpServlet;

import java.io.InputStream;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 记录Engine信息
 */
public class Engine {
    /**
     * Host配置映射表
     */
    private Map<String, Host> hostMap = new ConcurrentHashMap<>();

    /**
     * 添加虚拟主机
     *
     * @param hostName 虚拟主机名
     * @param host 要添加的虚拟主机
     */
    public void addHost(String hostName, Host host) {
        hostMap.put(hostName, host);
    }

    /**
     * 根据给定的URL，获取对应的Servlet
     *
     * @param url 给定的URL
     * @return 对应的Servlet
     */
    public HttpServlet resolveServlet(String url) {
        Host host = hostMap.get("localhost");
        if (host == null) {
            return null;
        }

        return host.resolveServlet(url);
    }

    /**
     * 根据给定的URL，获取对应的静态资源
     *
     * @param url 给定的URL
     * @return 对应的静态资源
     */
    public InputStream resolveStatic(String url) {
        Host host = hostMap.get("localhost");
        if (host == null) {
            return null;
        }

        return host.resolveStatic(url);
    }
}
